const express = require("express")
const router = express.Router()
const {addBlog, delBlog,updateBlog,searchBlog,updateSeeNum,updateGoodNum} = require("../control/blog")
const {SuccessModel,ErrorModel} = require("../model/resModel")


router.post("/add",async (req,res)=>{
  let data =await addBlog(req,res)
  console.log(data);
  
  if(data.affectedRows>0){
    res.json(new SuccessModel("添加成功"))
  }else{
    res.json(new ErrorModel("添加失败"))
  }
})
router.post("/del",async (req,res)=>{
  let data = await delBlog(req,res)
  console.log(data);
  if(data.affectedRows>0){
    res.json(new SuccessModel("删除成功"))
  }else{
    res.json(new ErrorModel("删除失败"))
  }
  
})
router.post("/update",async (req,res)=>{
  
  let data = await updateBlog(req,res)
  console.log(data);
  
  if(data.affectedRows>0){
    res.json(new SuccessModel("更新成功"))
  }else{
    res.json(new ErrorModel("更新失败"))
  }
  
})
router.post("/goodNum",async (req,res)=>{
  let data = await updateGoodNum(req,res)
  if(data.affectedRows>0){
    res.json(new SuccessModel("更新成功"))
  }else{
    res.json(new ErrorModel("更新失败"))
  }
})
router.post("/seeNum",async (req,res)=>{
  let data = await updateSeeNum(req,res)
  if(data.affectedRows>0){
    res.json(new SuccessModel("更新成功"))
  }else{
    res.json(new ErrorModel("更新失败"))
  }
})



router.get("/list",async (req,res)=>{
  let data = await searchBlog(req,res)
  if(data.length>0){
    res.json(new SuccessModel(data,"查找成功"))
  }else{
    res.json(new ErrorModel("查找失败"))
  }
  
})

module.exports=router